local PartnerEquipStrenghPop = class("PartnerEquipStrenghPop", app.mvc.ViewBase)
PartnerEquipStrenghPop.VIEW_ROOT_RES = Res.PartnerEquipStrength

local Item = require("app.public.dao.Item")
local IconRender = require("app.widget.renders.IconRender")
local PartnerEquipPropRender = import(".renders/PartnerEquipPropRender")
local Armature = require("app.public.avatar.Armature")
function PartnerEquipStrenghPop:init()
    display.setCascadeOpacityEnabled(self._root, true)
    self._ui.bg:setTouchEnabled(true)
    display.uiAddClick(self._ui.bg, function()
        self:closeView()
    end)
    self._ui.Button_close:addClickEventListener(function()
        self:closeView()
    end)

    self._ui["Panel/Panel_content/Panel_up/Button_s"]:addClickEventListener(handler(self, self._onBtnStrenth))
    self._ui["Panel/Panel_content/Panel_up/Button_all"]:addClickEventListener(handler(self, self._onBtnStrenthTen))
    self._equipIcon = IconRender.create(self._ui["Panel/Panel_content/Panel_mid/icon_1"])
    self._costIcon =  IconRender.create(self._ui["Panel/Panel_content/Panel_mid/icon_2"])
    self._equipIcon:setClickEnable(false)
    self._costIcon:setClickEnable(false)


    self._prop1 = PartnerEquipPropRender.create(self._ui["Panel/Panel_content/Panel_mid/data_1"])
    self._prop2 = PartnerEquipPropRender.create(self._ui["Panel/Panel_content/Panel_mid/data_2"])

    self:registerViewEvent({
        {name = ViewEvent.Partner_DataChange, callback = handler(self, self._onPartnerDataChange)},
        {name = ViewEvent.Partner_EquipStrengthResult,  callback = handler(self, self._onPartnerEquipStrengthResult)},
        {name = ViewEvent.Partner_EquipStrengthTenResult,  callback = handler(self, self._onPartnerEquipStrengthTenResult)}
        
    })

    self:refresh()
end

-- local lastReqTimeStamp = 0 -- 上次发送此请求的时间戳
-- local reqTimeOffset = 0.5
-- local lastPostTimeStamp = 0 -- 上次弹提示的时间戳
-- local postTimeOffset = 1
function PartnerEquipStrenghPop:_onBtnStrenth()
    if not self._data then
        return
    end
    local data = self._data

    if not data:checkCanStrength() then
        return
    end
    local curtime = app:getTime()
    if self._strenghOneTime then
        if curtime - self._strenghOneTime < 0.8 then
            return
        end
    end
	self._strenghOneTime = curtime

    UD.partnersData:C2S_EnhancePartnerEquipment(self._data:getId())
end


function PartnerEquipStrenghPop:_onBtnStrenthTen()
    if not self._data then
        return
    end
    local data = self._data

    if not data:checkCanStrength() then
        return
    end

    local curtime = app:getTime()
    if self._strenghTenTime then
        if curtime - self._strenghTenTime < 0.8 then
            return
        end
    end
	self._strenghTenTime = curtime

    UD.partnersData:C2S_EnhancePartnerEquipmentBatch(self._data:getId())
end

function PartnerEquipStrenghPop:refresh()

    if not self._data then
        return
    end
    
    local data = self._data
    self._ui["Panel/Panel_content/title"]:setString(data:getName())
    self._ui["Panel/Panel_content/title"]:setColor(Enums.QualityColor[ data:getQuality() ])

    local probability = data:getStrengthenProbability()
    local level = data:getStrengthenLevel()
    self._ui["Panel/Panel_content/Panel_up/bar/LoadingBar"]:setPercent(probability/100.0)

    self:setEquipCanStrength(data:checkCanStrength())

    self._equipIcon:setData(data)
    self._equipIcon:setLevelEx(0)

    local propData1 = {
        lv = level,
        props = data:getStrengthenPropShowDataByLevel(level)
    }
    local propData2

    self._ui["Panel/Panel_content/Panel_up/Text_MaxLv"]:setVisible(false)
    if data:isStrenthMaxLevel() then        
        self._costIcon:setVisible(false)
        self._ui["Panel/Panel_bg/line_01"]:setVisible(false)
        self._ui["Panel/Panel_content/Panel_bg/bg06"]:setVisible(false)
        self._ui["Panel/Panel_content/Panel_bg/bg07"]:setVisible(false)
        self._ui["Panel/Panel_content/Panel_mid/Text_2"]:setVisible(false)
        self._ui["Panel/Panel_content/Panel_mid/num"]:setVisible(false)
        self._prop2:setVisible(false)
        self._ui["Panel/Panel_content/Panel_up"]:setVisible(false)
        self._ui["Panel/Panel_content/Panel_mid/Text_Max"]:setVisible(true)
    else
        self._costIcon:setVisible(true)
        self._ui["Panel/Panel_bg/line_01"]:setVisible(true)
        self._ui["Panel/Panel_content/Panel_bg/bg06"]:setVisible(true)
        self._ui["Panel/Panel_content/Panel_bg/bg07"]:setVisible(true)
        self._ui["Panel/Panel_content/Panel_mid/Text_2"]:setVisible(true)
        self._ui["Panel/Panel_content/Panel_mid/num"]:setVisible(true)
        self._prop2:setVisible(true)
        self._ui["Panel/Panel_content/Panel_up"]:setVisible(true)
        self._ui["Panel/Panel_content/Panel_mid/Text_Max"]:setVisible(false)

        self._ui["Panel/Panel_content/Panel_up/num_gold"]:setString(string.format("%s%%", probability/100.0))
        
        propData2 = {
            lv = level+1,
            props = data:getStrengthenPropShowDataByLevel(level +1)
        }

        local costConf = data:getStrengthenUpCostByLevel(level)
        local nowNum = UD:getItemAmountByTid(costConf.id)
    
        local itemTemp = Item.new( costConf.type )
        itemTemp:read( { tid = costConf.id } )
            
        self._itemData = itemTemp
        self._costIcon:setVisible(true)
        self._costIcon:setData(itemTemp)
        self._costIcon:setNum(costConf.num)
        self._costIcon:setClickEnable(true)
        self._costIcon:setTipEnable(true)
        self._costIcon:setTipsCloseCallBack(function(tp)
            if tp == Enums.TIPS_HIDE.GOTO then
                executeFunc(self._onCloseView, self)
            end
        end)

        if costConf.num > nowNum then
            self._ui["Panel/Panel_content/Panel_mid/num"]:setColor(display.COLOR_RED)
        else
            self._ui["Panel/Panel_content/Panel_mid/num"]:setColor(display.COLOR_WHITE)
        end

        self._ui["Panel/Panel_content/Panel_mid/Text_2"]:setString(string.format(L("PartnerEquipStrengthNeedStr"), itemTemp:getName()))
        self._ui["Panel/Panel_content/Panel_mid/num"]:setString(string.format("%s/%s", nowNum, costConf.num))
    end
    self._prop1:setData(propData1)
    self._prop2:setData(propData2)
end


function PartnerEquipStrenghPop:updateProp()
end

function PartnerEquipStrenghPop:setEquipCanStrength(trueOrFalse)
    if trueOrFalse then
        self._ui["Panel/Panel_content/Panel_up/Button_s"]:setTouchEnabled(true)
        self._ui["Panel/Panel_content/Panel_up/Button_s"]:setBright(true)

        self._ui["Panel/Panel_content/Panel_up/Button_all"]:setTouchEnabled(true)
        self._ui["Panel/Panel_content/Panel_up/Button_all"]:setBright(true)
    else
        self._ui["Panel/Panel_content/Panel_up/Button_s"]:setTouchEnabled(false)
        self._ui["Panel/Panel_content/Panel_up/Button_s"]:setBright(false)

        self._ui["Panel/Panel_content/Panel_up/Button_all"]:setTouchEnabled(false)
        self._ui["Panel/Panel_content/Panel_up/Button_all"]:setBright(false)
    end
end

function PartnerEquipStrenghPop:_onPartnerDataChange()
    self:refresh()
end


function PartnerEquipStrenghPop:playStrengthSuccessAmi()
	if not self._successArmature then
		local params = {
			armatureName = "QH1",
			path = Res.EquipStrengthSuccessAmi
		}
		local armature = Armature.create(params)
		armature:setPosition(0, 0)
		self._ui["Panel/Panel_content/amiNode"]:addChild(armature)
		self._successArmature = armature
	end
	self._successArmature:play("QH1", 1)
end

function PartnerEquipStrenghPop:playStrengthFailAmi()
	if not self._failArmature then
		local params = {
			armatureName = "QH2",
			path = Res.EquipStrengthFailAmi
		}
		local armature = Armature.create(params)
		armature:setPosition(0, 0)
		self._ui["Panel/Panel_content/amiNode"]:addChild(armature)
		self._failArmature = armature
	end
	self._failArmature:play("QH2", 1)

	local aniNode = self._aniFailParticle
	if not aniNode then
		aniNode = cc.CSLoader:createNode(Res.EquipStrengthParticle, true)
		aniNode:setPosition(0, 0)
		self._ui.amiNode:addChild(aniNode)
		self._aniFailParticle = aniNode
	end

	local timeline = aniNode:getTimeline()
	local aniParticle = aniNode:findChild("Panel/Particle")
	timeline:gotoFrameAndPlay(0, false)
	aniParticle:resetSystem()
end

function PartnerEquipStrenghPop:_onPartnerEquipStrengthResult(eventName, ret)
    if ret then
        display.pushToast(L("tips_1070001"))
        self:playStrengthSuccessAmi()
    else
        self:playStrengthFailAmi()
    end
end

-- 强化十次 
function PartnerEquipStrenghPop:_onPartnerEquipStrengthTenResult(eventName, success, failed)
    local str = ""
    if success > 0 then
        str = str .. string.format(L("StrengthTenTipSuccess"), success)
    end

    if success >0 and failed > 0 then
        str = str .. ", "
    end

    if failed > 0 then
        str = str .. string.format(L("StrengthTenTipFail"), failed)
    end

    if success + failed > 0 then
        display.pushToast(str)
        if  success > 0 then
            self:playStrengthSuccessAmi()
        else
            if failed > 0 then
                self:playStrengthFailAmi()
            end
        end
    end
end


return PartnerEquipStrenghPop